package main.java.tmi.order.service.impl;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import main.java.tmi.order.service.OrderService;
import main.java.tmi.order.service.OrderVO;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;

/**
 * Test Service Implementation
 *
 * @author ssong
 * @since 2010. 09. 10.
 */
@Service
public class OrderServiceImpl implements OrderService {
	
	// Logger
	private Log logger;
	
	@Resource(name = "orderDAO")
	private OrderDAO orderDao;
	
	/**
	 * The Constructor
	 */
	public OrderServiceImpl() {
		logger = LogFactory.getLog(this.getClass());
	}
	

	
	//키값
	@Override
	public String getMaxOdrSeq(String ordNo1) throws Exception {
		// TODO Auto-generated method stub
		return orderDao.getMaxOdrSeq(ordNo1);
	}

	//키값
	@Override
	public String orderDupChk(String ordNo1) throws Exception {
		// TODO Auto-generated method stub
		return orderDao.orderDupChk(ordNo1);
	}
	
	
	//저장
	@Override
	public void addOrder(Map<String, String> param, HttpServletRequest request) throws Exception {
		// TODO Auto-generated method stub
		//오더 원장
		System.out.println("★★★★★★★★★"+param.get("ordRemark"));
		orderDao.addOrder(param);

		//오더 상세 		
		String goodsCodeTmp[] = request.getParameterValues("goodsCode");
		String qtyTmp[] 	  = request.getParameterValues("qty");
		String remarkTmp[]    = request.getParameterValues("remark");
		
		System.out.println("===========>goodsCodeTmp.length: "+ goodsCodeTmp.length);
		if(goodsCodeTmp.length > 0){
			Map detailParamMap = new HashMap();
			for (int i=0 ; i < goodsCodeTmp.length ; i++){
				
				if ( StringUtils.isNotEmpty(goodsCodeTmp[i]) ){
					detailParamMap.clear();	
					System.out.println("===========>goodsCodeTmp[i]: "+ goodsCodeTmp[i]);			
					detailParamMap.put("ordNo1", param.get("ordNo1"));
					detailParamMap.put("ordNo2", param.get("ordNo2"));
					detailParamMap.put("goodsCode", goodsCodeTmp[i]);
					detailParamMap.put("qty", qtyTmp[i] );
					detailParamMap.put("remark", StringUtils.defaultString(remarkTmp[i])) ;
					detailParamMap.put("sesseionId", param.get("sesseionId") );
					orderDao.addOrderDetail(detailParamMap);
				}
			}
		}
	}

	//수정
	@Override
	public boolean modifyOrder(Map<String, String> param, HttpServletRequest request) throws Exception {
		int resultCnt ;
		System.out.println("★★★★★★★★★"+param.get("ordRemark"));
		
		resultCnt = orderDao.modifyOrder(param);
		

		//오더 상세 추가 사항 저장
		String goodsCodeTmp[] = request.getParameterValues("goodsCode");
		String qtyTmp[] 	  = request.getParameterValues("qty");
		String remarkTmp[]    = request.getParameterValues("remark");

		if(goodsCodeTmp != null && !goodsCodeTmp.equals("")){
			Map detailParamMap = new HashMap();
			for (int i=0 ; i < goodsCodeTmp.length ; i++){
				
				if ( StringUtils.isNotEmpty(goodsCodeTmp[i]) ){
					detailParamMap.clear();	
					System.out.println("===========>goodsCodeTmp[i]: "+ goodsCodeTmp[i]);			
					detailParamMap.put("ordNo1", param.get("ordNo1"));
					detailParamMap.put("ordNo2", param.get("ordNo2"));
					detailParamMap.put("goodsCode", goodsCodeTmp[i]);
					detailParamMap.put("qty", qtyTmp[i] );
					detailParamMap.put("remark", StringUtils.defaultString(remarkTmp[i])) ;
					detailParamMap.put("sesseionId", param.get("sesseionId") );
					orderDao.addOrderDetail(detailParamMap);
				}
			}
		}
		
		return resultCnt > 0 ? true : false;
	}
	
	//조회
	@Override
	public Map<String,String> getOrderView(Map<String,String> param) throws Exception {

		return (Map<String,String>)orderDao.getOrderView(param);

		//return resultIdx > 0 ? true : false;
	}
	

	//삭제
	@Override
	public boolean deleteOrder(Map<String, String> param) throws Exception {
		int resultCnt ;
					orderDao.deleteOrderPdet(param);
					orderDao.deleteOrderDet(param);
		resultCnt = orderDao.deleteOrder(param);
		return resultCnt > 0 ? true : false;
	}
	
	
	/*============ ORDER DETAIL============*/
	
	//조회
	@Override
	public List<OrderVO> getOrderDetailList(Map<String,String> param) throws SQLException {
		List<OrderVO> list = orderDao.getOrderDetailList(param);
		System.out.println("====================="+list);
		return list;
	}
	
	//삭제
	@Override
	public boolean delOdrDetail(Map<String, String> param) throws Exception {
		int resultCnt ;
		resultCnt = orderDao.delOdrDetail(param);
		return resultCnt > 0 ? true : false;
	}
	
	//저장
	@Override
	public int addOrderDetail(Map<String, String> param) throws Exception {
		// TODO Auto-generated method stub
		System.out.println("===================>>>"+param);
		return orderDao.addOrderDetail(param);
	}
	
	//수정
	@Override
	public boolean detailUpdate(Map<String, String> param) throws Exception {
		int resultCnt ;
		resultCnt = orderDao.detailUpdate(param);
		return resultCnt > 0 ? true : false;
	}



	/*============ ORDER Division============*/
	//MAX값
    @Override
    public String getMaxOrdPseq(Map<String,String> param) throws Exception {
        // TODO Auto-generated method stub
        return orderDao.getMaxOrdPseq(param);
    }
	
	
	//0일때 조회
	@Override
	public List<OrderVO> getDefaultOrderDivisionList(Map<String,String> param) throws Exception {
		List<OrderVO> list = orderDao.getDefaultOrderDivisionList(param);
		System.out.println("====================="+list);
		return list;
	}

	 //0아닐 때 조회
    @Override
    public List<OrderVO> getLastOrderDivisionList(Map<String,String> param) throws Exception {
        List<OrderVO> list = orderDao.getLastOrderDivisionList(param);
        System.out.println("====================="+list);
        return list;
    }
    
    //0아닐 때 조회
   @Override
   public List<OrderVO> getNewOrderDivisionList(Map<String,String> param) throws Exception {
       List<OrderVO> list = orderDao.getNewOrderDivisionList(param);
       System.out.println("====================="+list);
       return list;
   }
   
   

	//저장
	@Override
	public void addOrderDivisionDetail(Map<String, String> param, HttpServletRequest request) throws Exception {
		// TODO Auto-generated method stub
	
		//분할 상세 		
		String confOrdSeqTmp[] = request.getParameterValues("confOrdSeq");
		String confGoodsCodeTmp[] = request.getParameterValues("confGoodsCode");
		String confQtyTmp[] = request.getParameterValues("confQty");
		
		System.out.println("===========>confOrdSeqTmp.length: "+ confOrdSeqTmp.length);
		if(confOrdSeqTmp.length > 0){
			Map divisionParamMap = new HashMap();

			divisionParamMap.put("confOrdNo1", param.get("confOrdNo1"));
			divisionParamMap.put("confOrdNo2", param.get("confOrdNo2"));
    		divisionParamMap.put("confOrdPseq", param.get("confOrdPseq"));
			divisionParamMap.put("sesseionId", param.get("sesseionId"));
			
			System.out.println("===========>confOrdSeqTmp.length: "+ confOrdSeqTmp.length);
			
			for (int i=0 ; i < confOrdSeqTmp.length ; i++){

				divisionParamMap.put("confOrdSeq", confOrdSeqTmp[i] );
				divisionParamMap.put("confGoodsCode", confGoodsCodeTmp[i] );
				divisionParamMap.put("confQty", confQtyTmp[i] );
				
				orderDao.addOrderDivisionDetail(divisionParamMap);
				
			}
		}
	}
   
	

	//수정
	@Override
	public boolean modifyOrderDivisionDetail(Map<String, String> param, HttpServletRequest request) throws Exception {
		// TODO Auto-generated method stub
		int resultCnt = 0;
		//분할 상세 		

		String confOrdSeqTmp[] = request.getParameterValues("confOrdSeq");
		String confQtyTmp[] = request.getParameterValues("confQty");
		
		System.out.println("===========>confOrdSeqTmp.length: "+ confOrdSeqTmp.length);
		if(confOrdSeqTmp.length > 0){
			Map divisionParamMap = new HashMap();

			divisionParamMap.put("confOrdNo1", param.get("confOrdNo1"));
			divisionParamMap.put("confOrdNo2", param.get("confOrdNo2"));
    		divisionParamMap.put("confOrdPseq", param.get("confOrdPseq"));
			divisionParamMap.put("sesseionId", param.get("sesseionId"));
					
			for (int i=0 ; i < confOrdSeqTmp.length ; i++){
				System.out.println("===========>confOrdSeqTmp : "+ confOrdSeqTmp[i]);
				divisionParamMap.put("confOrdSeq", confOrdSeqTmp[i] );
				divisionParamMap.put("confQty", confQtyTmp[i] );
				
				resultCnt = orderDao.modifyOrderDivisionDetail(divisionParamMap);
				
			}
		}
		
		return resultCnt > 0 ? true : false;
	}
	
	//삭제
	@Override
	public boolean deleteOrderDivisionDetail(Map<String, String> param) throws Exception {
		// TODO Auto-generated method stub
		int resultCnt = 0;
		//분할 상세 		

		Map divisionParamMap = new HashMap();

		divisionParamMap.put("confOrdNo1", param.get("confOrdNo1"));
		divisionParamMap.put("confOrdNo2", param.get("confOrdNo2"));
		divisionParamMap.put("confOrdPseq", param.get("confOrdPseq"));				

		String delChk = orderDao.deleteOrderDivisionChk(divisionParamMap);

		if(delChk.equals("Y")){
			resultCnt = orderDao.deleteOrderDivisionDetail(divisionParamMap);
		}
		return resultCnt > 0 ? true : false;
	}
	

}
